package com.xingkeduo.pojo.json;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.query.ShopsServprodQuery;
import com.xingkeduo.pojo.table.ShopsServprodTable;
import com.xingkeduo.tool.AmountUtils;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;
import javafx.scene.chart.PieChart;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

public class ShopsServprodJson extends BaseJson {

	public static ShopsServprodQuery createQueryForShopsServprod(String json) throws Throwable {
		ShopsServprodQuery query = new ShopsServprodQuery();
		JsonNode root = validJson(json);
		if (root == null)
			return query;
		query.setCode(validCode(root, YesNoEnum.YES));
		return query;
	}

	public static ObjectNode toJson(ShopsServprodTable shopsServprodTable) throws Exception {
		ObjectNode root = MAPPER.createObjectNode();

		if (shopsServprodTable != null)
		{
			root.put("id", shopsServprodTable.getId());
			root.put("code", shopsServprodTable.getCode());
			root.put("shopsId", shopsServprodTable.getShopsId());
			root.put("servprodId", shopsServprodTable.getServprodId());
			root.put("servtime", shopsServprodTable.getServtime());
			root.put("price", AmountUtils.changeF2Y(shopsServprodTable.getPrice()));

			//处理时间
			String createdTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(shopsServprodTable.getCreatedTime());
			root.put("createtime", createdTime);
		}

		return root;
	}

	public static ObjectNode toShopServprodJson(ShopsServprodTable shopsServprodTable) throws Exception {
		ObjectNode root = MAPPER.createObjectNode();

		if (shopsServprodTable != null)
		{
			root.put("shopsId", shopsServprodTable.getShopsId());
			root.put("servprodId", shopsServprodTable.getServprodId());
			root.put("servprodName", shopsServprodTable.getServprodName());
			root.put("servtime", shopsServprodTable.getServtime());
			root.put("price",AmountUtils.changeF2Y(shopsServprodTable.getPrice()));
			root.put("checked", shopsServprodTable.getChecked());
		}

		return root;
	}

	public static ArrayNode toJson(List<ShopsServprodTable> shopsServprodTableList) throws Throwable {
		ArrayNode root = MAPPER.createArrayNode();

		if(!CommonUtil.isEmpty(shopsServprodTableList)){
			for (Iterator<ShopsServprodTable> iterator = shopsServprodTableList.iterator(); iterator.hasNext();) {
				root.add(toShopServprodJson(iterator.next()));
			}
		}
		return root;
	}

	public static ShopsServprodTable createTableForModifyShopsServprod(String urlParam, ShopsServprodTable shopsServprodTable) throws Throwable {
		JsonNode root = validJson(urlParam);
		if (root == null)
			throw new InputParamException(InputParamException.Type.INVALID, "参数");

		shopsServprodTable.setId(validId(root, YesNoEnum.YES));

		return shopsServprodTable;
	}


}
